;; -*- emacs-lisp -*-

(add-to-list 'load-path "~/.emacs.d/Site-Lisp/auctex/")
(add-to-list 'auto-mode-alist '("\\.\\([Tt][Ee][Xx]\\)\\'" . TeX-latex-mode))
(add-to-list 'interpreter-mode-alist '(".tex" . TeX-latex-mode))

  ;(require 'tex-mik)
  (load "Site-Lisp/site-start.el")
;(require 'tex-site)
;(setq TeX-electric-escape t)

  (autoload 'reftex-mode     "reftex" "RefTeX Minor Mode" t) 
  (autoload 'turn-on-reftex  "reftex" "RefTeX Minor Mode" nil) 
  (autoload 'reftex-citation "reftex-cite" "Make citation" nil) 
  (autoload 'reftex-index-phrase-mode "reftex-index" "Phrase mode" t) 
  (add-hook 'LaTeX-mode-hook 'turn-on-reftex) ; with AUCTeX LaTeX mode 
  (add-hook 'latex-mode-hook 'turn-on-reftex) ; with Emacs latex mode  

(when (memq system-type '(windows-nt cygwin))
  (setq TeX-view-style '( ("." "yap -1 %dS %d")))
  (setq TeX-output-view-style
		'(("^dvi$" "." "yap -1 %dS %d"))))


(defun eip-latex-mod ()
  (setq auto-save-mode t) 
  (setq TeX-auto-save t) 
  (setq TeX-parse-self t)
  (setq-default TeX-master nil)
  (setq preview-preserve-counters t)

  (setq outline-minor-mode-prefix [(control o)])   

  (outline-minor-mode t)
  (auto-fill-mode t)
  (turn-on-reftex)
  (TeX-source-specials-mode t)

  (setq preview-scale-function 1.0)
  (setq blink-matching-paren nil)

  (add-to-list 'TeX-command-list `("DviPDF" "dvipdfmx %d" TeX-run-command t t  :help "Generate PDF from DVI"))
  (add-to-list 'TeX-command-list `("ViewPDF" "%s.pdf" TeX-run-discard t t  :help "Run PDF Viewer"))
  (add-to-list 'TeX-bar-TeX-button-alist
	       '(dvipdf :image "dvipdf"
			:command (TeX-command "DviPDF" 'TeX-master-file -1)
			:help (lambda (&rest ignored)
			(TeX-bar-help-from-command-list "DviPDF"))))
  (add-to-list 'TeX-bar-TeX-button-alist
	       '(viewpdf :image "viewpdf"
			 :command (TeX-command "ViewPDF" 'TeX-master-file -1)
			 :help (lambda (&rest ignored)
			(TeX-bar-help-from-command-list "ViewPDF"))))
  (add-to-list 'TeX-bar-LaTeX-button-alist
	       '(dvipdf :image "dvipdf"
			:command (TeX-command "DviPDF" 'TeX-master-file -1)
			:help (lambda (&rest ignored)
			(TeX-bar-help-from-command-list "DviPDF"))))
  (add-to-list 'TeX-bar-LaTeX-button-alist
	       '(viewpdf :image "viewpdf"
			 :command (TeX-command "ViewPDF" 'TeX-master-file -1)
			 :help (lambda (&rest ignored)
			(TeX-bar-help-from-command-list "ViewPDF"))))
  (add-to-list 'TeX-bar-TeX-buttons 'dvipdf t)
  (add-to-list 'TeX-bar-LaTeX-buttons 'dvipdf t)
  (add-to-list 'TeX-bar-TeX-buttons 'viewpdf t)
  (add-to-list 'TeX-bar-LaTeX-buttons 'viewpdf t)

  (define-key LaTeX-mode-map [(control meta \\)] 'fill-region)
  (define-key LaTeX-mode-map [(control meta \;)] 'hide-body)
  (define-key LaTeX-mode-map [(control \;)] 'outline-toggle-children)
; (define-key latex-mode-map (kbd "C-a \%") 'comment-region)
  (define-key LaTeX-mode-map [(tab)] 'my-indent-or-complete)
  (define-key LaTeX-mode-map [($)] 'auto-complete-paren)
  (define-key LaTeX-mode-map [(\{)] 'auto-complete-paren)
  (define-key LaTeX-mode-map [(\[)] 'auto-complete-paren)
  (define-key LaTeX-mode-map [(\()] 'auto-complete-paren)
)
  
(add-hook 'TeXinfo-mode-hook 'outline-minor-mode)   
(add-hook 'LaTeX-mode-hook 'outline-minor-mode)   
(add-hook 'LaTeX-mode-hook #'LaTeX-install-toolbar)

(add-hook 'latex-mode-hook 'eip-latex-mod)
(add-hook 'LaTeX-mode-hook 'eip-latex-mod)
